package com.shiguiwu.springboot3.feign;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Map;
import java.util.function.Supplier;

/**
 * @description: 动态url拦截器
 * @author: stone
 * @date: Created by 2023/7/30 22:05
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.feign
 */
public class DynamicUrlInterceptor implements RequestInterceptor {

    private final Supplier<OpenFeignDTO> urlSupplier;

    public DynamicUrlInterceptor(Supplier<OpenFeignDTO> urlSupplier) {
        this.urlSupplier = urlSupplier;
    }
    // standard constructor

    @Override
    public void apply(RequestTemplate template) {
        OpenFeignDTO dto = urlSupplier.get();
        if (dto.getUrl() != null && Boolean.TRUE.equals(dto.getIsSupport())) {
            template.target(dto.getUrl());
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    public static class OpenFeignDTO {
        /**
         * 拦截的url
         */
        private String url;

        /**
         * 该url是否拦截
         */
        private Boolean isSupport = false;
    }
}
